import { Component } from "react"
import src from './images/avatar.png'
import { formatTime } from './utils'
import './styles/index.css'
class App extends Component {
  state = {
    // hot: 热度排序  time: 时间排序
    tabs: [
      {
        id: 1,
        name: '热度',
        type: 'hot'
      },
      {
        id: 2,
        name: '时间',
        type: 'time'
      }
    ],
    content: '',
    active: 'hot',
    list: [
      {
        id: 1,
        author: '刘德华',
        comment: '给我一杯忘情水',
        time: new Date('2021-10-10 09:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: 1,
        imgsrc: '/刘德华.png'
      },
      {
        id: 2,
        author: '周杰伦',
        comment: '哎哟，不错哦',
        time: new Date('2021-10-11 09:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: 0,
        imgsrc: '/周杰伦.png'
      },
      {
        id: 3,
        author: '五月天',
        comment: '不打扰，是我的温柔',
        time: new Date('2021-10-11 10:09:00'),
        // 1: 点赞 0：无态度 -1:踩
        attitude: -1,
        imgsrc: '/五月天.png'
      }
    ]
  }
  changeTab = (type) => {
    this.setState({
      active: type
    })
  }
  del = (id) => {
    let newList = this.state.list.filter((item, index, array) => { return item.id !== id })
    this.setState({
      list: newList
    })
  }
  handleChange = (e) => {
    this.setState({
      content: e.target.value
    })
  }
  changeAttitude = (id, attitude) => {
    this.setState({
      list: this.state.list.map((item) => {
        if (item.id === id) {
          return {
            ...item,
            attitude,
          }
        } else {
          return item
        }
      }),
    })
  }
  addComment = () => {
    let newCom = {
      id: Date.now(),
      author: '作者',
      comment: this.state.content,
      time: new Date(),
      // 1: 点赞 0：无态度 -1:踩
      attitude: 0,
      imgsrc: '/avatar.png'
    }
    this.setState({
      list: [newCom, ...this.state.list],
      content: ''
    })
    console.dir(document.querySelector('.ipt-txt'))
    document.querySelector('.ipt-txt').value = ''
  }
  render () {
    return (
      <div className="App">
        <div className="comment-container">
          {/* 评论数 */}
          <div className="comment-head">
            <span>{this.state.list.length} 评论</span>
          </div>
          {/* 排序 */}
          <div className="tabs-order">
            <ul className="sort-container">
              {/* <li className="on">按热度排序</li>
          <li>按时间排序</li> */}
              {this.state.tabs.map((item, index, array) => { return <li key={item.id} onClick={() => { this.changeTab(item.type) }} className={item.type === this.state.active ? 'on' : ''}>按{item.name}排序</li> })}
            </ul>
          </div>

          {/* 添加评论 */}
          <div className="comment-send">
            <div className="user-face">
              <img className="user-head" src={src} alt="" />
            </div>
            <div className="textarea-container">
              <textarea onChange={this.handleChange}
                cols="80"
                rows="5"
                placeholder="发条友善的评论"
                className="ipt-txt"
              />
              <button className="comment-submit" onClick={this.addComment}>发表评论</button>
            </div>
            <div className="comment-emoji">
              <i className="face"></i>
              <span className="text">表情</span>
            </div>
          </div>

          {/* 评论列表 */}
          <div className="comment-list">
            {this.state.list.map((item, index, array) => {
              return <div className="list-item" key={item.id}>
                <div className="user-face">
                  <img className="user-head" src={require('./images' + item.imgsrc)} alt="" />
                </div>
                <div className="comment">
                  <div className="user">{item.author}</div>
                  <p className="text">{item.comment}</p>
                  <div className="info">
                    <span className="time">{formatTime(item.time)}</span>
                    <span onClick={() => {
                      this.changeAttitude(
                        item.id,
                        item.attitude === 1 ? 0 : 1
                      )
                    }}
                      className={item.attitude === 1 ? 'like liked' : 'like'}>
                      <i className="icon" />
                    </span>
                    <span onClick={() =>
                      this.changeAttitude(
                        item.id,
                        item.attitude === -1 ? 0 : -1
                      )
                    }
                      className={item.attitude === -1 ? 'hate hated' : 'hate'}>
                      <i className="icon" />
                    </span>
                    <span className="reply btn-hover" onClick={() => { this.del(item.id) }}>删除</span>
                  </div>
                </div>
              </div>
            })}
          </div>
        </div>
      </div >
    )
  }
}
export default App